package org.jeecg.modules.appuser.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.appuser.constant.LuckDrawConstant;
import org.jeecg.modules.appuser.constant.PayTypeConstant;
import org.jeecg.modules.appuser.entity.*;
import org.jeecg.modules.appuser.service.*;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 每天凌晨1点结束
 * </p>
 * 9/29
 */
@Slf4j
public class LuckDrawByWeekTask implements Job {

    @Autowired
    private ITbUserLuckdrawService tbUserLuckdrawService;

    @Autowired
    private ITbUserLuckdrawDetailService tbUserLuckdrawDetailService;

    @Autowired
    private ITbUserWalletService tbUserWalletService;

    @Autowired
    private ITbUserWalletDetailService tbUserWalletDetailService;

    /**
     * 定时任务执行入口
     */
    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        log.info("抽奖定时任务开始运行,dateTime:{}", DateUtils.now());
        //先暂停 抽奖，  抽取  用户， 如果没有用户，则作废这期，
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("state", 1);
        queryWrapper.eq("luckdraw_type", 2);//每日抽奖
        TbUserLuckdraw tbUserLuckdraw = tbUserLuckdrawService.getOne(queryWrapper);
        if (null != tbUserLuckdraw) {
            QueryWrapper bUserLuckdrawDetailQueryWrapper = new QueryWrapper();
            bUserLuckdrawDetailQueryWrapper.eq("luckdraw_id", tbUserLuckdraw.getId());
            List<TbUserLuckdrawDetail> tbUserLuckdrawDetailList = tbUserLuckdrawDetailService.list();
            if (null != tbUserLuckdrawDetailList && tbUserLuckdrawDetailList.size() > 0) {
                //抽奖人数
                int person_number = tbUserLuckdrawDetailList.size();
                int random_number = getRandom(person_number);
                TbUserLuckdrawDetail tbUserLuckdrawDetail = tbUserLuckdrawDetailList.get(random_number - 1);
                String user_id = tbUserLuckdrawDetail.getUserId();
                tbUserLuckdraw.setLuckUserId(user_id);
                tbUserLuckdraw.setState(0);
                tbUserLuckdraw.setReleaseStatus(1);
                if (tbUserLuckdrawService.updateById(tbUserLuckdraw)) {
                    log.info("------恭喜-----6se---中将---------------"+user_id);
                }
            }
        }


        log.info("抽奖客定时任务开始结束,dateTime:{}", DateUtils.now());
    }


    private void saveUserWalletDetail(TbUserWallet tbUserWallet, BigDecimal newBalance) {
        TbUserWalletDetail tbUserWalletDetail = new TbUserWalletDetail();
        tbUserWalletDetail.setPayFrom(PayTypeConstant.PAYFORM_LUCKDRAW);
        tbUserWalletDetail.setPayType(PayTypeConstant.PAYTYPE_INCOME);
        tbUserWalletDetail.setPayNumber(newBalance);
        tbUserWalletDetail.setWalletId(tbUserWallet.getId());
        tbUserWalletDetail.setUserId(tbUserWallet.getUserId());
        tbUserWalletDetailService.save(tbUserWalletDetail);
    }

    public static int getRandom(int max) {

        int min = 1;
        Random random = new Random();
        int s = random.nextInt(max) % (max - min + 1) + min;
        return s;
    }
}